package com.mxchip.livestarmobile.mobile.ui.contact


import android.app.Dialog
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.mxchip.livestarmobile.mobile.util.Constant
import com.mxchip.livestarmobile.mobile.util.Utils
import com.mxchip.hxb3288a.base.hideKeyboard
import com.mxchip.livestarmobile.mobile.base.toast
import com.mxchip.livestarmobile.R
import com.mxchip.livestarmobile.mobile.state.SystemData
import kotlinx.android.synthetic.main.fragment_add_member.*


class AddMemberFragment : DialogFragment() {

    private var contactId: Long = -1L
    private val contactViewModel by viewModels<ContactViewModel>()


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)

        dialog.setCanceledOnTouchOutside(false)

        return dialog
    }


    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_add_member, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val numberArg = arguments?.let { AddMemberFragmentArgs.fromBundle(it).number }
        val nameArg = arguments?.let { AddMemberFragmentArgs.fromBundle(it).name }

        arguments?.let { AddMemberFragmentArgs.fromBundle(it).id }?.let { contactId = it }

        numberArg?.let {

            if(it.length == Constant.LOCAL_CODE_LENGTH) {
                val shortNumber = it.trimStart('0')

                Log.e("AddMemberFragment", "shortNumber: $shortNumber")

                input_number.setText(shortNumber)
            }

            else {
                input_number.setText(it)
            }
        }

        nameArg?.let {
            input_name.setText(it)
        }

        if (contactId == -1L) {
            text_title.text = "新建联系人"
        } else {
            text_title.text = "更新联系人"
        }


        save_button.setOnClickListener {

            val name = input_name.text.toString()
            val number = input_number.text.toString()

            if (TextUtils.isEmpty(name)) {
                toast("姓名为空")
                return@setOnClickListener
            } else if (TextUtils.isEmpty(number)) {
                toast("号码为空")
                return@setOnClickListener
            }

            val dialNumber = if (number.length <= Constant.LOCAL_CODE_LENGTH) {
                number.padStart(8, '0')
            } else {
                number
            }

            if (SystemData.userId() == dialNumber.trim() || SystemData.userId() == dialNumber) {
                toast("被叫为本机号码")
                return@setOnClickListener
            }
            if (Utils.getUserId() == dialNumber) {
                toast("存储号码为本机号码")
                return@setOnClickListener
            }

            if (contactId == -1L) {
                contactViewModel.createContact(name, dialNumber)
            } else {
                Log.e("testInsert", "testInsert $contactId, $name, $dialNumber")
                contactViewModel.updateContact(contactId.toString(), name, dialNumber)
            }
            hideKeyboard()
            findNavController().popBackStack()
        }

        button_cancel.setOnClickListener {

            hideKeyboard()
            findNavController().popBackStack()
        }
    }


}
